﻿var validationContext;

function ValidateForm(/*formID*/) {
    Sys.Mvc.FormContext._Application_Load();
    //var form = $("#" + formID);
    var form = $("form");
    //alert($(form).length);
    validationContext = Sys.Mvc.FormContext.getValidationForForm($(form)[$(form).length - 1]);

    if (validationContext != null) {
        for (var i = 0; i < validationContext.fields.length; i++) {

            var controlToValidate = $(validationContext.fields[i].elements[0]);
            var controlToValidateID = $(controlToValidate).attr("id");
            var image = $('img[name="img' + controlToValidate.attr("name") + '"]');
            var titleContainer = $('div[name="' + controlToValidate.attr("name") + '.title.container"');
            if ($(image).length > 0 && $(titleContainer).length > 0) {
                $(controlToValidate).bind("change", DoValidationEvent);
                if ($(titleContainer).hasClass("tooltipAbove")) {
                    $(image).tooltip({ tip: "#" + $(titleContainer).attr("id"), relative: true });
                }
                else {
                    $(image).tooltip({ tip: "#" + $(titleContainer).attr("id"), relative: true, position: 'bottom center' });
                }
            }
        }

        $(form).submit(function () {
            DoValidations(true);
        });

        DoValidations(false);
    }
}

function DoValidationEvent() {
    var fieldErrors;
    var id = $(this).attr("id");
    for (var i = 0; i < validationContext.fields.length; i++) {
        if ($(validationContext.fields[i].elements[0]).attr("id") == id) {
            fieldErrors = validationContext.fields[i].validate();
            ShowValidationImage(i, fieldErrors);
            $(this).trigger("change2"); // Para validaciones custom utilizar el evento change2 
        }
    }
}

function DoValidations(showImages) {
    var pageValidated = true;
    var fieldErrors;
    for (var i = 0; i < validationContext.fields.length; i++) {
        fieldErrors = validationContext.fields[i].validate();
        if (showImages) {
            ShowValidationImage(i, fieldErrors);
        }
        if (fieldErrors.length > 0) {
            pageValidated = false;
        }
    }
    return pageValidated;
}

function ShowValidationImage(fieldOrder, fieldErrors) {
    var control = $(validationContext.fields[fieldOrder].elements[0]);
    var id = $(control).attr("id");
    var name = $(control).attr("name");
    var controlByName;
    var controlByID;
    var isSelect;
    var image;
    var div;

    isSelect = false;
    controlByName = $('input[name="' + name + '"]');
    if (controlByName.length > 0) {
        control = controlByName[0];
        image = $('img[name="img' + name + '"]');
        div = $('div[name="' + name + '.title.container"');
    }
    else {
        controlByName = $('select[name="' + name + '"]');
        if (controlByName.length > 0) {
            isSelect = true;
            control = controlByName[0];
            image = $('img[name="img' + name + '"]');
            div = $('div[name="' + name + '.title.container"');
        }
    }

    if ($(control).length > 0) {
        if ($(control)[0].nodeName.toLowerCase() == "input") {
            if (fieldErrors.length > 0) {
                var validationText = $(validationContext.fields[fieldOrder].validationMessageElement).text();
                $(div).html(validationText);
                if (isSelect) {
                    //$(control).removeClass("styled");
                }
                $(control).addClass("form-input5");
                $(control).removeClass("form-input");
                $(image).show();
            }
            else {
                if (isSelect) {
                    //$(control).addClass("styled");
                }
                $(control).addClass("form-input");
                $(control).removeClass("form-input5");
                $(image).hide();
            }
        }
        else {
            if (isSelect) {
                if (fieldErrors.length > 0) {
                    var validationText = $(validationContext.fields[fieldOrder].validationMessageElement).text();
                    $(div).html(validationText);
                    //$(control).addClass("form-input5");
                    //$(control).removeClass("form-input");
                    //$(control).removeClass("styled");
                    $(image).show();
                }
                else {
                    //$(control).addClass("styled");
                    //$(control).addClass("form-input");
                    //$(control).removeClass("form-input5");
                    $(image).hide();
                }
            }
        }
    }
}

function ShowCustomValidationImage(controlToValidate, errorMessage) {
    var image = $('img[name="img' + controlToValidate.attr("name") + '"]');
    var titleContainer = $('div[name="' + controlToValidate.attr("name") + '.title.container"]');
    if (errorMessage.length > 0) {
        $(controlToValidate).addClass("form-input5");
        $(controlToValidate).removeClass("form-input");
        $(titleContainer).html(errorMessage);
        $(image).show();
    }
    else {
        $(controlToValidate).removeClass("form-input5");
        $(controlToValidate).addClass("form-input");
        $(titleContainer).html("");
        $(image).hide();
    }
}
